#include "selfBullet.h"
#include "gameMath.h"

Point* selfBullet::nowSelect = NULL;

void selfBullet::setPoint(Point* p)
{
	nowSelect = p;
}

selfBullet::selfBullet(stdSelfBullet std, Point p, int power, int kind)
{
	setInformation(std, p, power, kind);
}

void selfBullet::setInformation(stdSelfBullet std, Point p, int power, int kind)
{
	_std = std;
	_std.bitmap.p = p;
	_std.power = power;
	_kind = kind;
	vy = -_std.spd;
	vx = 0;
	_reMove = false;
	_angle = 0;
}

selfBullet::selfBullet(stdSelfBullet std, Point p,
					   int power, int kind, int angle)
{
	setInformation(std, p, power, kind, angle);
}

void selfBullet::setInformation(stdSelfBullet std, Point p,
	int power, int kind, int angle)
{
	_std = std;
	_std.bitmap.p = p;
	_std.power = power;
	_kind = kind;
	vy = -_std.spd;
	vx = 0;
	_reMove = false;
	_angle = angle;
}

void selfBullet::draw()
{
	float ra = angleToRad(_angle);
	/*al_draw_bitmap(_std.bitmap.bitmap[0],
		_std.bitmap.p.x - _std.bitmap.w,
		_std.bitmap.p.y - _std.bitmap.h, 0);*/
	
	if (_kind == FOLLOW)
	{
		ra = ALLEGRO_PI - ra;
		al_draw_rotated_bitmap(_std.bitmap.bitmap[0],_std.bitmap.w,
			_std.bitmap.h, _std.bitmap.p.x,
			_std.bitmap.p.y, ra, 0);
	}else
	{
		ra = ALLEGRO_PI - ra;
		al_draw_rotated_bitmap(_std.bitmap.bitmap[0],_std.bitmap.w,
			_std.bitmap.h, _std.bitmap.p.x,
			_std.bitmap.p.y, ra, 0);
	}
}

void selfBullet::updata()
{
	checkOut();
	if (_kind == FOLLOW)
	{
		calcSpd();
	}
	if (_angle < 0)
	{
		vx = -_std.spd * SIN[-_angle];
		vy = _std.spd * COS[-_angle];
	}else 
	{
		vx = _std.spd * SIN[_angle];
		vy = _std.spd * COS[_angle];
	}
	move();
}

void selfBullet::move()
{
	_std.bitmap.p.x += vx;
	_std.bitmap.p.y += vy;
}

void selfBullet::calcSpd()
{
	//cout << nowSelect << endl;
	if (nowSelect != NULL)
	{
		//cout << nowSelect->x << " " << nowSelect->y << endl;
		if (nowSelect->x > 1 && nowSelect->y > 1)
		{
			/*float t = (nowSelect->x - _std.bitmap.p.x)*(nowSelect->x - _std.bitmap.p.x)
				+ (nowSelect->y - _std.bitmap.p.y)*(nowSelect->y - _std.bitmap.p.y);
			t = InvSqrt(t);
			vx = _std.spd * t * (nowSelect->x - _std.bitmap.p.x);
			vy = _std.spd * t * (nowSelect->y - _std.bitmap.p.y);*/
			_angle = al_atan(nowSelect->x - _std.bitmap.p.x,
			nowSelect->y - _std.bitmap.p.y);
		}
	}
}

void selfBullet::checkOut()
{
	if (_std.bitmap.p.x > 640 || _std.bitmap.p.x < 0)
	{
		_reMove = true;
	}
	if (_std.bitmap.p.y > 480 || _std.bitmap.p.y < 0)
	{
		_reMove = true;
	}
}